<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏绘制</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('/geofence')">返回</el-button>
  </div>
  <!-- 准备应该div充当地图容器 -->
  <div id="mapContainer" style="width: 100%;height: 84vh;">

  </div>
  <!-- 创建一个创建并保存围栏数据的弹窗 -->
  <el-dialog title="新增围栏" v-model="dialogVisible">
    <el-form>
      <el-form-item label="围栏名称">
        <el-input v-model="geofence.name" placeholder="请输入围栏名称"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="saveGeofence">确定</el-button>
    </template>
  </el-dialog>


</template>


<script setup>
import router from "@/router";
import {nextTick, onMounted, ref} from "vue";
import {DrawScene, DrawControl, OperateEventType} from 'bmap-draw';
import {ElMessage} from "element-plus";
import qs from "qs";
import axios from "axios";
// 创建一个geofence对象,用于保存围栏数据
const geofence = ref({
  name: '',//围栏名称
  position: {}//围栏数据,是一个对象
});
//
const dialogVisible = ref(false);

let map;
//一进入页面就立即加载地图
onMounted(() => {
  //表示DOM更新完毕后立即执行的方法,为了确保地图加载时,地图容器div已经创建完毕
  nextTick(() => {
    //创建地图实例,注意:忽略导入语句!不需要导入!
    map = new BMapGL.Map("mapContainer");
    //设置地图展示的中心点,此处的坐标是天安门的经纬度
    let point = new BMapGL.Point(116.4074, 39.9024);
    //设置地图对象的中心点与缩放级别
    map.centerAndZoom(point, 15);

    // 添加鼠标绘制控件
    const scene = new DrawScene(map);
    const drawContrl = new DrawControl(scene, {
      enableTips: false,
      anchor: BMAP_ANCHOR_TOP_RIGHT,
      drawingItems: [
        'circle',
        'rectangle'
      ]
    });
    scene.addEventListener(OperateEventType.COMPLETE, (event) => {
      /* 当用户点√让弹窗出现 */
      dialogVisible.value = true;

      let overlay = event.target.overlay;
      console.log(overlay)

      if (overlay instanceof BMapGL.Circle) {
        geofence.value.position.type = 'circle';
        geofence.value.position.radius = overlay.getRadius();
        geofence.value.position.longiyude = overlay.getCenter().lng;
        geofence.value.position.latitude = overlay.getCenter().lat;
        console.log(geofence.value)
      } else {
        geofence.value.position.type = 'rectangle';
        let recPoints = []
        for (let i = 0; i < 4; i++) {
          recPoints.push(overlay.points[i].latLng.lng + '-' + overlay.points[i].latLng.lat);
        }
        geofence.value.position.recPoints = recPoints.join(',');
        console.log(geofence.value)
      }
    })
    map.addControl(drawContrl);
  })
})

const saveGeofence = () => {
  if (!geofence.value.name || geofence.value.name.trim() == '') {
    ElMessage.error('请输入围栏名称!');
    return;
  }
  // 由于position为一个对象,先将position对象转换为JSON字符串
  geofence.value.position = JSON.stringify(geofence.value.position);
  let data = qs.stringify(geofence.value)

  axios.post(BASE_URL + '/v1/geofence/save', data).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('新增围栏成功');
      dialogVisible.value = false;//关闭弹窗
      router.push('/geofence');//返回列表页面
    } else {
      ElMessage.error(response.data.message);
    }
  })

}
// 关闭弹窗
const closeDialog = () => {
  if (confirm('确定关闭吗?')) {
    map.clearOverlays();//清空地图
    geofence.value = {
      name: '',
      position: {}
    }
    dialogVisible.value = false;
  }
}

</script>

<style scoped>

</style>